class Solution {
public:
    bool searchMatrix(vector<vector<int>>& matrix, int target) {
        int l = 0;
        int r = matrix[0].size() - 1;
        int sz = matrix.size();
        int i = 0;
        while (i < sz && matrix[i][r] < target)
            ++i;
        if (i == sz)
            return false;
        while (l <= r) {
            int mid = (l + r) / 2;
            int t = matrix[i][mid];
            if (t == target)
                return true;
            else if (t > target)
                r = mid - 1;
            else
                l = mid + 1;
        }
        return false;
    }
};